import {
	getJsapiTicket,
	getAgentJsapiTicket,
} from '@/api/index.js'

import * as ww from '@wecom/jssdk'

let AgentJsapi = ''
let Jsapi = ''
let corpId = ''
let agentId = ''


async function jsapi() {
	let params = {
		url: process.env.VUE_APP_BACKURL,
		corp_id: corpId
	}
	let res = await getJsapiTicket(params)
	Jsapi = res.data
}
async function agenJstApi() {
	let params = {
		url: h5Url,
		corp_id: corpId
	}
	let res = await getAgentJsapiTicket(params)
	//console.log(res.data,'agenJstApi');
	AgentJsapi = res.data
}

async function wwRegister(jsApi) {
	return new Promise((resolve, reject) => {
		ww.register({
			// suiteId:'wx03555b37f0f48ef9',
			//corpId: 'wxe0f0b473eecd6a6d',
			corpId: corpId,
			agentId: agentId,
			jsApiList: jsApi,
			getConfigSignature(url) {
				//console.log(this.Jsapi, 'Jsapi')
				return {
					timestamp: Jsapi.timestamp,
					nonceStr: Jsapi.nonceStr,
					signature: Jsapi.signature
				}
			},
			getAgentConfigSignature(url) {
				//console.log(AgentJsapi, 'AgentJsapi')
				// uni.showToast({
				// 	icon:'none',
				// 	title: AgentJsapi,
				// })
				return {
					timestamp: AgentJsapi.timestamp,
					nonceStr: AgentJsapi.nonceStr,
					signature: AgentJsapi.signature
				}
			},
			onAgentConfigSuccess(res) {
				//alert(JSON.stringify(res))
				//alert(888)
			},
			onAgentConfigFail(res) {
				//console.log(JSON.stringify(res), 'onAgentConfigFail');
				//alert(JSON.stringify(res))
			},
			onAgentConfigComplete(res) {
				//alert(JSON.stringify(res)+jsApi)
			},
			onConfigSuccess(res) {
				//alert(JSON.stringify(res)+jsApi)
				// uni.showToast({
				// 	icon:'none',
				// 	title: '成功',
				// })
				console.log(res, '成功')
				//alert(JSON.stringify(res))
			},
			onConfigFail(res) {
				//alert()

				//alert(JSON.stringify(res))
				//alert('jj')
			},
			onConfigComplete(res) {
				//alert(JSON.stringify(res)+jsApi)
				//alert(JSON.stringify(res))
			}
		})
		resolve()
	})
}
async function register(jsApi) {
	await getConfig()
	await jsapi()
	await agenJstApi()
	//if(jsApi.isArray()){
	//await wwRegister(jsApi.join(','))
	//}else{
	await wwRegister(jsApi)
	//}

}
export default register